<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜品编辑</title>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <script type="text/javascript" src="../layui/layui.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
</head>
<body>
<div class="container">
    <form class="layui-form" lay-filter="formTest" id="addForm" method="post">
        <input id="foodId" name="foodId" type="hidden"/>
        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">
                菜名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="foodName" name="foodName" required lay-verify="required"
                       placeholder="请输入菜名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属菜系</label>
            <div class="layui-input-block layui-form" lay-filter="foodclass" style="width:190px">
                <select id="select2" name="foodclass" resource lay-verify="required">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                菜品简介
            </label>
            <div class="layui-input-inline">
                <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                推荐星级
            </label>
            <div id="rate"></div>
        </div>
        <input id="rate1" name="rate1" type="hidden"/><!--隐藏域-->
        <div class="layui-form-item">
            <label class="layui-form-label">
                价格
            </label>
            <div class="layui-input-inline">
                <input type="number" id="price" name="price" min="0.0" step="0.1" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" key="set-mine" lay-filter="save" lay-submit>
                保存
            </button>
        </div>
    </form>
</div>
<script>
    $(function () {
        $("#select2").html("");
        $.post("../findAllFoodClassName.do", function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#select2").append('<option value="' + data[i] + '">' + data[i] + '</option>');
            }
        }, "json")
    })
    layui.use(['form', 'layer', 'rate'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var rate = layui.rate;
        var id = GetQueryString("foodId");
        var ss = 1;
        $.ajax({
            url: "../findFooddetailByFoodId.do",
            data: {"foodId": id},
            success: function (data) {
                form.val("formTest", {
                    "foodName": data.foodName // "name": "value"
                    , "foodclass": data.foodclass.className
                    , "desc": data.foodDetail
                    , "price": data.foodPrice
                    , "rate1": data.foodEvaluate
                    , "foodId": data.foodId
                })
                ss = $("#rate1").val();
            },
            dataType: "json",
            error: function () {
                layer.alert("系统出错");
            },
            async: false
        });
        form.render('select', 'foodclass');

        form.on('submit(save)', function (data) {
            var foodName = data.field.foodName;//表格数据
            var foodclass = data.field.foodclass;//表格数据
            var desc = data.field.desc;//表格数据
            var rate1 = data.field.rate1;//表格数据
            var price = data.field.price;//表格数据
            var foodId = data.field.foodId;//表格数据
            $.post("../updateFoodDetail.do", {
                "foodId": foodId,
                "foodName": foodName,
                "foodclass": foodclass,
                "desc": desc,
                "rate": rate1,
                "price": price
            }, function (data) {
                if (data.success) {
                    layer.alert(data.message, {icon: 6}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.layui.table.reload('fooddetail');
                    });
                } else {
                    layer.alert(data.message, {icon: 3}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                }
            }, "json");

            return false;
        });
        rate.render({
            elem: '#rate',  //绑定元素
            length: 5,
            text: true,
            setText: function (value) {
                $("#rate1").val(value);
            },
            value: ss
        });
    });

    //定义此方法可以获取到URL路径上传值
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");//获取参数值的正则表达式
        var urlKeys = window.location.search.substr(1);//获取url传参中？后面的值：id=123&name=小明
        urlKeys = decodeURI(urlKeys);//针对中文乱码的解决方案；decodeURI()解决乱码问题
        var r = urlKeys.match(reg);
        if (r != null) {
            return r[2];
        } else {
            return null;
        }
    }
</script>
</body>
</html>